﻿using ZyGames.Framework.Game.Contract;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Tools;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using System;
using ZyGames.Framework.Common.Serialization;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 请求保存的牌谱
	/// </summary>
	public class Action10065 : BaseAction
	{
		private int userId;
		private string savedTableRecordStr = string.Empty;
		private bool result = false;
		private string log = "";
		public Action10065(HttpGet actionGetter)
			: base((short)ActionType.DZApplySavedRecord, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("UserId", ref userId))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			var currentGameUser = GameTable.Current.GetUser(userId);
			if (currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息10063玩家不存在");
				log = "信息有误";
				result = false;
				return true;
			}
			//获得我的保存的记录
			var sharedTable = GameTable.Current.SharedUserSavedTable(userId);
			
			var savedHandTables = new List<SavedHandTable>();
			foreach(var t in sharedTable)
			{
				int profitScore = 0;
				int smallBind = 0;
				DateTime beginTime;
				var score = GameTable.Current.GetScoreInTableHand(t.UserID, t.TableID, t.Hand);
				if(score == null)
					continue;
				else
					profitScore = score.Profit;

				var tableData = GameTable.Current.GetGameTableData(t.TableID);
				if(tableData == null)
					continue;
				else
				{
					beginTime = tableData.StartTime;
					smallBind = tableData.SmallBind;
				}

				savedHandTables.Add(new SavedHandTable() { Table = t, Score = profitScore, StartTime = beginTime, SmallBind = smallBind });
			}
			savedTableRecordStr = Convert.ToBase64String(ProtoBufUtils.Serialize(savedHandTables));
			result = true;
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(this.result);
			this.PushIntoStack(this.log);
			this.PushIntoStack(this.savedTableRecordStr);
		}
	}
}
